
Public Class COrder
    Inherits BOBase

    Protected m_State As OrderDS
    Protected m_DA As Orders
    Private m_OrderItemState As OrderItemDS
    Private m_OrderItems As ArrayList
    Private m_userBarCode As String
    Private m_userCountry As String

#Region "   Inherits Implementation"
    Sub New()
        m_State = New OrderDS
        m_OrderItemState = New OrderItemDS
        m_DA = New Orders
        m_OrderItems = New ArrayList
    End Sub

    Public Overrides ReadOnly Property DA() As DABase
        Get
            If m_DA Is Nothing Then
                m_DA = New Orders
            End If

            Return m_DA
        End Get
    End Property

    Public Overrides Property ObjectState() As DataSet
        Get
            Return m_State
        End Get
        Set(ByVal Value As DataSet)
            Try
                m_State = CType(Value, OrderDS)
            Catch ex As InvalidCastException
                Throw New ApplicationException("Invalid ORDER Dataset!", ex)
            End Try
        End Set
    End Property

    Public Overrides Sub Init()
        Dim dr As OrderDS.ORDERRow = m_State.ORDER.NewORDERRow
        With dr
            .ORDER_TYPE = 0
            .ORDER_NUMBER = String.Empty
            .CUSTOMER_ID_FK = 0
            .DESTINATION_PORT_ID_FK = 0
            .SUPPLIER_ID_FK = 0
            .SORT_REF_ID_FK = 0
            .SORT_REF2_ID_FK = 0
            .SetSORT_RANGE_ID_FKNull()
            .REQUESTED_ETA = Nothing
            .SetETA_DATE1Null()
            .SetETA_DATE2Null()
            .CONTAINERS = 1
            .CONTAINER_TYPE = EContainerType._40_feet
            .PACKING_TYPE = EPackingType.Pallet
            .SHIPPING_TYPE = EShippingType.Customer_Nominated
            .ORDER_DATE = Now
            .ORDER_BY_ID_FK = 0
            .SetSHIPPING_INSTRUCTIONSNull()
            .STATUS = EOrderStatus.Active
            .SetCREATED_DATENull()
            .SetCREATED_BYNull()
            .SetUPDATED_DATENull()
            .SetUPDATED_BYNull()
            .SetADJUSTED_APRROVED_BY_ID_FKNull()
            .SetADJUSTED_BY_ID_FKNull()
            .SetADJUSTED_DATENull()
            .SetAPPROVAL_DATENull()
            .ORDER_ITEM_TYPE = EOrderItemType.Independent_PO
        End With
        m_State.ORDER.Rows.Add(dr)
    End Sub
#End Region

#Region "   Data Properties"
    Public ReadOnly Property UserBarCode() As String
        Get
            If IsNothing(Me.m_userBarCode) OrElse Me.m_userBarCode.Trim = "" Then
                If Me.CustomerID > 0 Then
                    Dim obj As New CUser
                    obj.Load(Me.CustomerID)
                    Me.m_userBarCode = obj.BarCode
                End If
            End If
            Return Me.m_userBarCode
        End Get
    End Property

    Public ReadOnly Property UserCountry() As String
        Get
            If IsNothing(Me.m_userCountry) OrElse Me.m_userCountry.Trim = "" Then
                If Me.CustomerID > 0 Then
                    Dim obj As New CUser
                    obj.Load(Me.CustomerID)
                    Me.m_userCountry = obj.Country
                End If
            End If
            Return Me.m_userCountry
        End Get
    End Property

    Public ReadOnly Property OrderItems() As ArrayList
        Get
            Return Me.m_OrderItems
        End Get
    End Property

    Public ReadOnly Property OrderItem(ByVal index As Integer) As COrderItem
        Get
            Return CType(m_OrderItems(index), COrderItem)
        End Get
    End Property

    Protected ReadOnly Property DataRow() As OrderDS.ORDERRow
        Get
            Return CType(Me.Row, OrderDS.ORDERRow)
        End Get
    End Property

    Public ReadOnly Property ID() As System.Int32
        Get
            Return Me.DataRow.ORDER_ID_PK
        End Get
    End Property

    Public Property OrderType() As System.Byte
        Get
            Return Me.DataRow.ORDER_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.ORDER_TYPE = Value
        End Set
    End Property

    Public Property OrderNumber() As System.String
        Get
            Return Me.DataRow.ORDER_NUMBER
        End Get
        Set(ByVal Value As System.String)
            Me.DataRow.ORDER_NUMBER = Value
        End Set
    End Property

    Public Property CustomerID() As System.Int32
        Get
            Return Me.DataRow.CUSTOMER_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.CUSTOMER_ID_FK = Value
        End Set
    End Property

    Public ReadOnly Property DestinationPort() As System.String
        Get
            Return Common.GetLKDValue(Me.DestinationPortLkdID, 1)
        End Get
    End Property

    Public Property DestinationPortLkdID() As System.Int32
        Get
            Return Me.DataRow.DESTINATION_PORT_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.DESTINATION_PORT_ID_FK = Value
        End Set
    End Property

    Public ReadOnly Property SupplierName() As System.String
        Get
            Return Common.GetLKDValue(Me.SupplierLkdID, 2)
        End Get
    End Property

    Public ReadOnly Property SupplierCode() As System.String
        Get
            Return Common.GetLKDValue(Me.SupplierLkdID, 1)
        End Get
    End Property

    Public Property SupplierLkdID() As System.Int32
        Get
            Return Me.DataRow.SUPPLIER_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.SUPPLIER_ID_FK = Value
        End Set
    End Property

    'Public ReadOnly Property SortReference() As System.String
    '    Get
    '        Return Common.GetLKDValue(Me.SortReferenceLkdID, 1)
    '    End Get
    'End Property

    'Public Property SortReferenceLkdID() As System.Int32
    '    Get
    '        Return Me.DataRow.SORT_REF_ID_FK
    '    End Get
    '    Set(ByVal Value As System.Int32)
    '        Me.DataRow.SORT_REF_ID_FK = Value
    '    End Set
    'End Property

    Public ReadOnly Property SortReference() As System.String
        Get
            If Me.DataRow.IsSORT_REF_ID_FKNull Then
                Return Nothing
            Else
                Return Common.GetLKDValue(Me.SortReferenceLkdID, 1)
            End If
        End Get
    End Property

    Public Property SortReferenceLkdID() As System.Int32
        Get
            If Me.DataRow.IsSORT_REF_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.SORT_REF_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetSORT_REF_ID_FKNull()
            Else
                Me.DataRow.SORT_REF_ID_FK = Value
            End If
        End Set
    End Property

    Public ReadOnly Property SortReference2() As System.String
        Get
            If Me.DataRow.IsSORT_REF2_ID_FKNull Then
                Return Nothing
            Else
                Return Common.GetLKDValue(Me.SortReference2LkdID, 1)
            End If
        End Get
    End Property

    Public Property SortReference2LkdID() As System.Int32
        Get
            If Me.DataRow.IsSORT_REF2_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.SORT_REF2_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetSORT_REF2_ID_FKNull()
            Else
                Me.DataRow.SORT_REF2_ID_FK = Value
            End If
        End Set
    End Property

    Public ReadOnly Property SortRange() As System.String
        Get
            If Me.DataRow.IsSORT_RANGE_ID_FKNull Then
                Return Nothing
            Else
                Return Common.GetLKDValue(Me.SortRangeLkdID, 1)
            End If
        End Get
    End Property


    Public Property SortRangeLkdID() As System.Int32
        Get
            If Me.DataRow.IsSORT_RANGE_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.SORT_RANGE_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetSORT_RANGE_ID_FKNull()
            Else
                Me.DataRow.SORT_RANGE_ID_FK = Value
            End If
        End Set
    End Property

    Public Property RequestedETA() As System.Byte
        Get
            Return Me.DataRow.REQUESTED_ETA
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.REQUESTED_ETA = Value
        End Set
    End Property

    Public Property ETADate1() As System.DateTime
        Get
            If Me.DataRow.IsETA_DATE1Null Then
                Return Nothing
            Else
                Return Me.DataRow.ETA_DATE1
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetETA_DATE1Null()
            Else
                Me.DataRow.ETA_DATE1 = Value
            End If
        End Set
    End Property

    Public Property ETADate2() As System.DateTime
        Get
            If Me.DataRow.IsETA_DATE2Null Then
                Return Nothing
            Else
                Return Me.DataRow.ETA_DATE2
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetETA_DATE2Null()
            Else
                Me.DataRow.ETA_DATE2 = Value
            End If
        End Set
    End Property

    Public Property Containers() As System.Int32
        Get
            Return Me.DataRow.CONTAINERS
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.CONTAINERS = Value
        End Set
    End Property

    Public Property ContainerType() As System.Byte
        Get
            Return Me.DataRow.CONTAINER_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.CONTAINER_TYPE = Value
        End Set
    End Property

    Public Property PackingType() As System.Byte
        Get
            Return Me.DataRow.PACKING_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.PACKING_TYPE = Value
        End Set
    End Property

    Public Property ShippingType() As System.Byte
        Get
            Return Me.DataRow.SHIPPING_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.SHIPPING_TYPE = Value
        End Set
    End Property

    Public Property OrderDate() As System.DateTime
        Get
            Return Me.DataRow.ORDER_DATE
        End Get
        Set(ByVal Value As System.DateTime)
            Me.DataRow.ORDER_DATE = Value
        End Set
    End Property

    Public Property OrderByCustomerID() As System.Int32
        Get
            Return Me.DataRow.ORDER_BY_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.ORDER_BY_ID_FK = Value
        End Set
    End Property

    Public Property ShippingInstructions() As System.String
        Get
            If Me.DataRow.IsSHIPPING_INSTRUCTIONSNull Then
                Return Nothing
            Else
                Return Me.DataRow.SHIPPING_INSTRUCTIONS
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetSHIPPING_INSTRUCTIONSNull()
            Else
                Me.DataRow.SHIPPING_INSTRUCTIONS = Value
            End If
        End Set
    End Property

    Public Property Status() As System.Byte
        Get
            If Me.DataRow.IsSTATUSNull Then
                Return Nothing
            Else
                Return Me.DataRow.STATUS
            End If
        End Get
        Set(ByVal Value As System.Byte)
            If Value = Nothing Then
                Me.DataRow.SetSTATUSNull()
            Else
                Me.DataRow.STATUS = Value
            End If
        End Set
    End Property

    Public Property CreatedDate() As System.DateTime
        Get
            If Me.DataRow.IsCREATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_DATENull()
            Else
                Me.DataRow.CREATED_DATE = Value
            End If
        End Set
    End Property

    Public Property CreatedBy() As System.Int32
        Get
            If Me.DataRow.IsCREATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_BYNull()
            Else
                Me.DataRow.CREATED_BY = Value
            End If
        End Set
    End Property

    Public Property UpdatedDate() As System.DateTime
        Get
            If Me.DataRow.IsUPDATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_DATENull()
            Else
                Me.DataRow.UPDATED_DATE = Value
            End If
        End Set
    End Property

    Public Property UpdatedBy() As System.Int32
        Get
            If Me.DataRow.IsUPDATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_BYNull()
            Else
                Me.DataRow.UPDATED_BY = Value
            End If
        End Set
    End Property

    Public Property AdjustedByID() As System.Int32
        Get
            If Me.DataRow.IsADJUSTED_BY_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.ADJUSTED_BY_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetADJUSTED_BY_ID_FKNull()
            Else
                Me.DataRow.ADJUSTED_BY_ID_FK = Value
            End If
        End Set
    End Property

    Public Property AdjustedDate() As System.DateTime
        Get
            If Me.DataRow.IsADJUSTED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.ADJUSTED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetADJUSTED_DATENull()
            Else
                Me.DataRow.ADJUSTED_DATE = Value
            End If
        End Set
    End Property

    Public Property AdjustedApprovedByID() As System.Int32
        Get
            If Me.DataRow.IsADJUSTED_APRROVED_BY_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.ADJUSTED_APRROVED_BY_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetADJUSTED_APRROVED_BY_ID_FKNull()
            Else
                Me.DataRow.ADJUSTED_APRROVED_BY_ID_FK = Value
            End If
        End Set
    End Property

    Public Property AdjustedApprovedDate() As System.DateTime
        Get
            If Me.DataRow.IsAPPROVAL_DATENull() Then
                Return Nothing
            Else
                Return Me.DataRow.APPROVAL_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetAPPROVAL_DATENull()
            Else
                Me.DataRow.APPROVAL_DATE = Value
            End If
        End Set
    End Property

    Public Property OrderItemType() As System.Byte
        Get
            Return Me.DataRow.ORDER_ITEM_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.ORDER_ITEM_TYPE = Value
        End Set
    End Property

    Public Property CompleteInfo() As System.Boolean
        Get
            If Me.DataRow.IsCOMPLETE_INFONull Then
                Return Nothing
            Else
                Return Me.DataRow.COMPLETE_INFO
            End If
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetCOMPLETE_INFONull()
            Else
                Me.DataRow.COMPLETE_INFO = Value
            End If
        End Set
    End Property

    Public Property DepartureDate() As System.DateTime
        Get
            If Me.DataRow.IsDEPARTURE_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.DEPARTURE_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetDEPARTURE_DATENull()
            Else
                Me.DataRow.DEPARTURE_DATE = Value
            End If
        End Set
    End Property

    Public Property Destination() As System.String
        Get
            If Me.DataRow.IsDESTINATIONNull Then
                Return Nothing
            Else
                Return Me.DataRow.DESTINATION
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetDESTINATIONNull()
            Else
                Me.DataRow.DESTINATION = Value
            End If
        End Set
    End Property

    Public Property FreightID() As System.Int32
        Get
            If Me.DataRow.IsFREIGHT_BY_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.FREIGHT_BY_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetFREIGHT_BY_ID_FKNull()
            Else
                Me.DataRow.FREIGHT_BY_ID_FK = Value
            End If
        End Set
    End Property

    Public Property ContainerNumber() As System.String
        Get
            If Me.DataRow.IsCONTAINER_NUMBERNull Then
                Return Nothing
            Else
                Return Me.DataRow.CONTAINER_NUMBER
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetCONTAINER_NUMBERNull()
            Else
                Me.DataRow.CONTAINER_NUMBER = Value
            End If
        End Set
    End Property

    Public Property Comments() As System.String
        Get
            If Me.DataRow.IsCOMMENTSNull Then
                Return Nothing
            Else
                Return Me.DataRow.COMMENTS
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetCONTAINER_NUMBERNull()
            Else
                Me.DataRow.COMMENTS = Value
            End If
        End Set
    End Property

#End Region

#Region "   Private members"
    Private Function BuildOICollection()
        Me.m_OrderItems.Clear()
        Dim i As Integer
        For i = 0 To Me.m_OrderItemState.ORDER_ITEM.Rows.Count - 1
            If Me.m_OrderItemState.ORDER_ITEM(i).RowState <> DataRowState.Deleted Then
                Me.m_OrderItems.Add(New COrderItem(Me.m_OrderItemState, i, Me.UserBarCode, Me.UserCountry))
            End If
        Next
    End Function
#End Region

#Region "   Overrides methods"
    Public Overloads Overrides Sub Load(ByVal keys() As Object)
        MyBase.Load(keys)

        Dim daOI As New OrderItems
        daOI.SelectByOrderID(keys(0), m_OrderItemState, Me.m_OrderItemState.ORDER_ITEM.TableName)

        BuildOICollection()
    End Sub

    Public Overloads Overrides Sub Save()

        Me.DA.BeginTrans()
        Try
            MyBase.Save()

            Dim i As Integer
            For i = 0 To Me.m_OrderItems.Count - 1
                If Me.OrderItem(i).Row.RowState <> DataRowState.Deleted Then
                    ' assign ID for non-deleted row
                    Me.OrderItem(i).OrderId = Me.ID
                End If
            Next

            If Me.OrderItems.Count > 0 Then
                ' save child table, only need to save the first order item
                ' because "Save" in DABase mean Update a whole dataset
                Me.OrderItem(0).Save(CType(Me.DA.Transaction, SqlClient.SqlTransaction))
            End If

            Me.DA.CommitTrans()
        Catch ex As Exception
            Me.DA.RollBackTrans()
            Throw ex
        End Try

    End Sub

    'Public Overloads Overrides Sub Save()

    '    Me.DA.BeginTrans()
    '    Try
    '        MyBase.Save()
    '        Me.DA.CommitTrans()

    '        Me.DA.BeginTrans()
    '        Dim i As Integer
    '        For i = 0 To Me.m_OrderItems.Count - 1
    '            If Me.OrderItem(i).Row.RowState <> DataRowState.Deleted Then
    '                ' assign ID for non-deleted row
    '                Me.OrderItem(i).OrderId = Me.ID
    '            End If
    '        Next

    '        If Me.OrderItems.Count > 0 Then
    '            ' save child table, only need to save the first order item
    '            ' because "Save" in DABase mean Update a whole dataset
    '            Me.OrderItem(0).Save(CType(Me.DA.Transaction, SqlClient.SqlTransaction))
    '        End If

    '        Me.DA.CommitTrans()
    '    Catch ex As Exception
    '        Me.DA.RollBackTrans()
    '        Throw ex
    '    End Try

    'End Sub

    Public Overloads Overrides Sub Remove()
        MyBase.Remove()

        'doesn't need to remove child table rows, 'cause we were enabled cascade delete in DB
        Me.m_OrderItems.Clear()
        Me.m_OrderItemState.Clear()
    End Sub
#End Region

#Region "   Public methods"
    Public Function AddNewOrderItem() As Integer
        Dim row As OrderItemDS.ORDER_ITEMRow = Me.m_OrderItemState.ORDER_ITEM.NewORDER_ITEMRow
        With row
            .ORDER_ID_FK = Nothing
            .PRODUCT_CODE_ID_FK = Nothing
            .COLOR_ID_FK = Nothing
            .PACKING = Nothing
            .QUANTITY = Nothing
            .IS_FIXED = Nothing
            .SetSPECIAL_INSTRUCTIONSNull()
            .SetUSE_FROM_BO_ITEMNull()
            .SetADJUSTED_QUANTITYNull()
        End With
        Me.m_OrderItemState.ORDER_ITEM.AddORDER_ITEMRow(row)

        Me.m_OrderItems.Add(New COrderItem(Me.m_OrderItemState, m_OrderItems.Count, Me.UserBarCode, Me.UserCountry))
        Return m_OrderItems.Count - 1
    End Function

    Public Sub RemoveOrderItem(ByVal id As Integer)
        Dim row As OrderItemDS.ORDER_ITEMRow = Me.m_OrderItemState.ORDER_ITEM.FindByOI_ID_PK(id)
        If Not row Is Nothing Then
            If row.RowState = DataRowState.Added Then
                Me.m_OrderItemState.ORDER_ITEM.Rows.Remove(row)
            Else
                row.Delete()
            End If

            Me.BuildOICollection()
        End If
    End Sub

    Public Function GetProductCodeList() As DataTable
        Dim al As New ArrayList
        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("ProductCode")

        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If Not al.Contains(OrderItem(i).ProductCode) Then
                al.Add(OrderItem(i).ProductCode)
                Dim row As DataRow
                row = dt.NewRow
                row(0) = OrderItem(i).ID
                row(1) = OrderItem(i).ProductCode
                dt.Rows.Add(row)
            End If
        Next

        Return dt
    End Function

    Public Function GetAdjustableQuantityProductCodeList() As DataTable
        Dim al As New ArrayList
        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("ProductCode")

        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If Me.OrderItem(i).IsFixed = False Then
                If Not al.Contains(OrderItem(i).ProductCode) Then
                    al.Add(OrderItem(i).ProductCode)
                    Dim row As DataRow
                    row = dt.NewRow
                    row(0) = OrderItem(i).ID
                    row(1) = OrderItem(i).ProductCode
                    dt.Rows.Add(row)
                End If
            End If
        Next

        Return dt
    End Function

    'Public Function GetProductionUpdatableProductCodeList() As DataTable
    '    Return Me.GetProductCodeList
    'End Function


    Public Function GetProductColorList(ByVal productCode As String) As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("OrderItemID")
        dt.Columns.Add("Color")

        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If OrderItem(i).ProductCode = productCode Then
                Dim row As DataRow
                row = dt.NewRow
                row(0) = OrderItem(i).ID
                row(1) = OrderItem(i).Color
                dt.Rows.Add(row)
            End If
        Next

        Return dt
    End Function

    Public Function GetAdjustableQuantityProductColorList(ByVal productCode As String) As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Index")
        dt.Columns.Add("Color")

        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If OrderItem(i).IsFixed = False AndAlso OrderItem(i).ProductCode = productCode Then
                Dim row As DataRow
                row = dt.NewRow
                row(0) = i
                row(1) = OrderItem(i).Color
                dt.Rows.Add(row)
            End If
        Next

        Return dt
    End Function

    Public Function GetOrderList(ByVal customerID As Integer, ByVal orderNo As String, ByVal status As Byte, ByVal startRecord As Integer, ByVal maxRecords As Integer) As DataSet
        Return Me.m_DA.GetOrderList(customerID, orderNo, status, startRecord, maxRecords)
    End Function

    Public Function GetOrderListByCusIDSupIDSortRefSortRangeOrderNo(ByVal customerID As Integer, ByVal supID As Integer, ByVal sortRef As Integer, ByVal sortRange As Integer, ByVal orderNo As String, ByVal status As Byte, ByVal startRecord As Integer, ByVal maxRecords As Integer) As DataSet
        Return Me.m_DA.GetOrderListByCusIDSupIDSortRefSortRangeOrderNo(customerID, supID, sortRef, sortRange, orderNo, status, startRecord, maxRecords)
    End Function

    Public Function GetOrderHistoryReportByType(ByVal startDate As DateTime, ByVal endDate As DateTime, ByVal CustomerID As Integer, ByVal SupplierCode As Integer) As DataSet
        Return Me.m_DA.GetOrderHistoryReportByType(startDate, endDate, CustomerID, SupplierCode)
    End Function

    Public Function GetOrderHistoryReportByProduct(ByVal startDate As DateTime, ByVal endDate As DateTime, ByVal CustomerID As Integer, ByVal productCode As String) As DataSet
        Return Me.m_DA.GetOrderHistoryReportByProduct(startDate, endDate, CustomerID, productCode)
    End Function

    Public Function GetOrderHistoryReportByProduct(ByVal startDate As DateTime, ByVal endDate As DateTime, ByVal CustomerID As Integer, ByVal productCode As Integer) As DataSet
        Return Me.m_DA.GetOrderHistoryReportByProduct(startDate, endDate, CustomerID, productCode)
    End Function

    Public Shared Function GetListPOUsed(ByVal OrderItemID As Integer) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetListPOUsed", OrderItemID)
        Return ds.Tables(0)
    End Function

    Public Function IsAllQuantityFixed() As Boolean
        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If Me.OrderItem(i).IsFixed = False Then
                Return False
            End If
        Next
        Return True
    End Function

    Public Function IsIndependentPO() As Boolean
        Dim i As Integer
        For i = 0 To Me.m_OrderItems.Count - 1
            If Me.OrderItem(i).UseFromBoItem <> Nothing Then
                Return False
            End If
        Next
        Return True
    End Function

#End Region

#Region "   Utilities shared function"
    'Public Shared Function GetOrderList() As DataTable
    '    Dim ds As New DataSet
    '    ds = DataHelper.ExecuteDataSet2("GetOrderList")        
    '    Return ds.Tables(0)
    'End Function

    Public Shared Function GetAllBulkOrder(Optional ByVal customerID As Integer = 0) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetAllBulkOrder", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetAllBulkOrderAndIndependentPO(Optional ByVal customerID As Integer = 0) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetAllBulkOrderAndIndependentPO", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetAllPurchaseOrderActiveForCustomer(ByVal customerID As Integer) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetAllPurchaseOrderActiveForCustomer", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetCustomerListFromNonRemovedOrders() As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetCustomerListFromNonRemovedOrders")
        Return ds.Tables(0)
    End Function

    Public Shared Function GetCustomerListFromOrders() As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetCustomerListFromOrders")
        Return ds.Tables(0)
    End Function

    Public Shared Function GetSupplierListFromOrders(ByVal customerID As Integer, Optional ByVal activeOrderOnly As Boolean = True) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetSupplierListFromOrders", customerID, activeOrderOnly)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetSortRangeListFromOrders(ByVal customerID As Integer) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetSortRangeListFromOrders", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetSortRefListFromOrders(ByVal customerID As Integer) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetSortRefListFromOrders", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetETAPortListFromOrders(ByVal customerID As Integer) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetETAPortListFromOrders", customerID)
        Return ds.Tables(0)
    End Function

    Public Shared Function UpdateCompleteInformation(ByVal orderID As Integer, ByVal departureDate As System.DateTime, ByVal destination As String, ByVal freightID As Integer, ByVal containerNumber As String, ByVal comments As String) As Integer
        Try
            Return DataHelper.ExecuteNonQuery("UpdateCompleteInformation", orderID, True, departureDate, destination, freightID, containerNumber, comments)
        Catch ex As Exception
            Throw New ApplicationException("Order Complete Information has been changed unsuccessfully!")
        End Try
    End Function

    Public Shared Function PerRemoveOrder(ByVal orderID As Integer) As Integer
        Try
            Return DataHelper.ExecuteNonQuery("PermanentlyRemoveOrder", orderID)
        Catch ex As Exception
            Throw New ApplicationException("Couldn't permanently remove the order! The order maybe in use. Please try again later or contact system administrator.")
        End Try
    End Function
#End Region

    Public Overloads Overrides Sub Save(ByVal transaction As System.Data.SqlClient.SqlTransaction)

    End Sub
End Class

#Region "   Order's Enums"

Public Enum EOrderType As Byte
    Purchase_Order
    Bulk_Order
End Enum

Public Enum EContainerType As Byte
    _20_feet
    _40_feet
    _40_feet_high_cube
End Enum

Public Enum EPackingType As Byte
    Loose
    Pallet
End Enum

Public Enum EShippingType As Byte
    Customer_Nominated
    Supplier_Nominated
End Enum

Public Enum ERequestedETAType As Byte
    As_soon_as_possible
    Before
    After
    Between
End Enum

Public Enum EOrderStatus As Byte
    Active
    Archived
    Removed
End Enum

Public Enum EOrderItemType As Byte
    Use_from_BO_only
    Independent_PO
End Enum
#End Region